//
//  RHLocalChoiceParaView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/9.
//

#import "RHLocalChoiceParaView.h"
@interface RHLocalChoiceParaView()
@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) UIView *contentView;

@property (nonatomic ,strong) UIPickerView *pickerView;

@property (nonatomic ,strong) MASConstraint *bgViewTopConstraint;

@property (nonatomic ,strong) MASConstraint *bgViewBottomConstraint;

@end
@implementation RHLocalChoiceParaView

-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self addSubview:touchView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *contentView = kView;
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = 8;
    contentView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);
       
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setText:@"请选择".rh_localized];
    [_titleLab setTextAlignment:NSTextAlignmentCenter];
    [_titleLab setFont:[UIFont systemFontOfSize:16]];
    [_titleLab setTextColor:FontColor(@"#969799")];
    _titleLab.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(49);
        make.top.mas_offset(0);
    }];
    
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    sureBtn.titleLabel.font = kSYSTEMFONT(15);
    [sureBtn setTitleColor:FontColor(@"#969799") forState:UIControlStateNormal];
    [contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titleLab);
        make.right.mas_offset(-15);
        make.height.offset(40);
    }];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:FontColor(@"#eeeeee")];
    [contentView addSubview:lineV];
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(1);
        make.top.equalTo(weakSelf.titleLab.mas_bottom);
    }];
    
    _pickerView = [[UIPickerView alloc]init];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [contentView addSubview:_pickerView];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineV.mas_bottom);
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(0);
        make.height.offset(45*4);
    }];
    
    
    
    
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    

    
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        
        [weakSelf hidden];
    }];
    
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.curParaModel){
            if (weakSelf.choiceResultBlock) {
                weakSelf.choiceResultBlock(weakSelf.curParaModel);
            }
            [weakSelf hidden];
        }
    }];
    
}


- (void)setCurParaModel:(RHLocalSettingModel *)curParaModel{
    _curParaModel = curParaModel;
    __block NSInteger index = -1;
    kWeakSelf;
    [curParaModel.selectList enumerateObjectsUsingBlock:^(RHLocalSettingOptionModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([self.curParaModel.content integerValue] == obj.textValue){
            index = obj.textValue;
            [weakSelf.pickerView selectRow:idx inComponent:0 animated:NO];
            *stop = YES;
        }
    }];
    //如果没有值,设一个默认值
    if(index == -1){
        [weakSelf.pickerView selectRow:0 inComponent:0 animated:NO];
        _curParaModel.content = [_curParaModel.enumNames firstObject];
    }
}

- (void)hidden{
    kWeakSelf;
    [self.bgViewBottomConstraint uninstall];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);

    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {
      
        [self removeFromSuperview];
       
    }];
}


- (void)showAnimation{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.bgViewTopConstraint uninstall];
            [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                self.bgViewBottomConstraint = make.bottom.mas_offset(0);

            }];
            [UIView animateWithDuration:0.2 animations:^{
                [self setNeedsLayout];
                [self layoutIfNeeded];
                [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
            }];
    });

}


#pragma mark ------ pickView delegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   
    return self.curParaModel.enumNames.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSString *content = self.curParaModel.enumNames[row];
    
    
    return content.rh_localized;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = [UIFont systemFontOfSize:15];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
//        pickerLabel.textColor = [UIColor blueColor];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    RHLocalSettingOptionModel *optionModel = self.curParaModel.selectList[row];
    self.curParaModel.content = [NSString stringWithFormat:@"%ld",optionModel.textValue];
    
}

-(void)dealloc{
    NSLog(@"---->ChoiceParaView dealloc");
}
@end
